package com.example.mjc.guaguaka;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by mjc on 2016/3/17.
 */
public class GgView extends View {
    private Paint mPaint;
    private Paint mCoverPaint;
    private Bitmap mBitmap;
    private Bitmap mCoverBitmap;

    private int mLastX;
    private int mLastY;
    private Path mPath;
    private Canvas c;
    private Canvas mCanvas;
    private int width;
    private int height;

    public GgView(Context context) {
        super(context);
        init(context);
    }

    public GgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(40);
        mPaint.setStyle(Paint.Style.STROKE);

        mCoverPaint = new Paint();
        mCoverPaint.setAntiAlias(true);
        mCoverPaint.setStrokeWidth(40);
        mCoverPaint.setStyle(Paint.Style.STROKE);

        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.kn).copy(Bitmap.Config.ARGB_8888, true);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        mCoverBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        c = new Canvas(mCoverBitmap);
        mCoverPaint.setStyle(Paint.Style.FILL);
        mCoverPaint.setColor(Color.GRAY);
        c.drawRoundRect(new RectF(0, 0, width, height), 30, 30,
                mCoverPaint);
        mCoverPaint.setDither(true);
//        c.drawBitmap(BitmapFactory.decodeResource(getResources(),
//                R.mipmap.s_title), null, new RectF(0, 0, width, height), null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int w = getMeasuredWidth();
        int h = getMeasuredHeight();
        int bW = mBitmap.getWidth();
        int bH =mBitmap.getHeight();

        float minScale =Math.min((float) bW / w, (float) bH / h);

        canvas.drawBitmap(mBitmap,(int)(bW/2-bW*minScale/2),(int)(bH/2-bH*minScale/2),mPaint);

        mCoverPaint.setStyle(Paint.Style.STROKE);
        mCoverPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        c.drawPath(mPath, mCoverPaint);
        canvas.drawBitmap(mCoverBitmap,0,0,null);

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mLastX = x;
                mLastY = y;
                mPath.moveTo(mLastX, mLastY);
                break;
            case MotionEvent.ACTION_MOVE:

                int dx = Math.abs(x - mLastX);
                int dy = Math.abs(y - mLastY);

                if (dx > 3 || dy > 3)
                    mPath.lineTo(x, y);

                mLastX = x;
                mLastY = y;
                break;
        }

        invalidate();
        return true;
    }

}
